home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / manchest.lha / MANCHESTER / manchester / 2.2 / Games-Dice.st < prev    next >
Text File  |  1993-07-24  |  5KB  |  226 lines

  1. "    NAME        Games-Dice
  2.     AUTHOR        TPH@cs.man.ac.uk
  3.     FUNCTION alternative decision-making 
  4.     ST-VERSIONS    2.2
  5.     PREREQUISITES     
  6.     CONFLICTS    
  7.     DISTRIBUTION      world
  8.     VERSION        1.1
  9.     DATE    22 Jan 1989
  10. SUMMARY    Games-Dice
  11.     is a simple alternative to conventional decision-making!!
  12.     (2.2). TPH
  13. "!
  14. Model subclass: #Dice
  15.     instanceVariableNames: 'value randomGenerator '
  16.     classVariableNames: ''
  17.     poolDictionaries: ''
  18.     category: 'Games-Dice'!
  19. Dice comment:
  20. 'I represent a single Dice (strictly, a DIE).  I respond to the roll
  21. message to generate a new random number.'!
  22.  
  23.  
  24. !Dice methodsFor: 'initialize-release'!
  25.  
  26. initialize
  27.     "Initialize the random number generator, and set up the initial
  28.      state of the receiver."
  29.  
  30.     randomGenerator _ Random new.
  31.     self rollDice! !
  32.  
  33. !Dice methodsFor: 'accessing'!
  34.  
  35. value
  36.     "Answer with the value represented by the receiver."
  37.  
  38.     ^value! !
  39.  
  40. !Dice methodsFor: 'modifying'!
  41.  
  42. roll
  43.     "Update the dice value randomly several times, to give the impression
  44.      of a real roll."
  45.  
  46.     6 timesRepeat: [
  47.         self rollDice.
  48.         self changed]!
  49.  
  50. rollDice
  51.     "Update the dice value randomly."
  52.  
  53.     value _ (randomGenerator next * 6 + 1) truncated! !
  54. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  55.  
  56. Dice class
  57.     instanceVariableNames: ''!
  58.  
  59.  
  60. !Dice class methodsFor: 'instance creation'!
  61.  
  62. new
  63.     "Answer with a new instance of the receiver."
  64.  
  65.     ^super new initialize! !
  66.  
  67. MouseMenuController subclass: #DiceController
  68.     instanceVariableNames: ''
  69.     classVariableNames: 'DiceYellowButtonMenu DiceYellowButtonMessages '
  70.     poolDictionaries: ''
  71.     category: 'Games-Dice'!
  72. DiceController comment:
  73. 'I represent the controller for a DiceView.'!
  74.  
  75.  
  76. !DiceController methodsFor: 'initialize-release'!
  77.  
  78. initialize
  79.     "Initialize the yellow button menu."
  80.  
  81.     super initialize.
  82.     self
  83.         yellowButtonMenu: DiceYellowButtonMenu
  84.         yellowButtonMessages: DiceYellowButtonMessages! !
  85.  
  86. !DiceController methodsFor: 'control defaults'!
  87.  
  88. isControlActive
  89.     ^(view containsPoint: sensor cursorPoint) & sensor blueButtonPressed not! !
  90.  
  91. !DiceController methodsFor: 'menu messages'!
  92.  
  93. redButtonActivity
  94.  
  95.     self model roll.
  96.     self sensor waitNoButton!
  97.  
  98. rollDice
  99.  
  100.     self model roll! !
  101. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  102.  
  103. DiceController class
  104.     instanceVariableNames: ''!
  105.  
  106.  
  107. !DiceController class methodsFor: 'class initialization'!
  108.  
  109. initialize
  110.     "DiceController initialize."
  111.  
  112.     DiceYellowButtonMenu _ PopUpMenu
  113.         labels: 'roll' withCRs.
  114.     DiceYellowButtonMessages _ #(rollDice).! !
  115.  
  116. DiceController initialize!
  117.  
  118.  
  119. View subclass: #DiceView
  120.     instanceVariableNames: 'cacheBox cachedForms '
  121.     classVariableNames: ''
  122.     poolDictionaries: ''
  123.     category: 'Games-Dice'!
  124. DiceView comment:
  125. 'I know how to display a Dice in a viewable manner.  I keep cached forms
  126. for speed.'!
  127.  
  128.  
  129. !DiceView methodsFor: 'initialize-release'!
  130.  
  131. initialize
  132.     "Initialize the array for cached forms."
  133.  
  134.     cachedForms _ Array new: 6.
  135.     super initialize! !
  136.  
  137. !DiceView methodsFor: 'controller access'!
  138.  
  139. defaultControllerClass
  140.  
  141.     ^DiceController! !
  142.  
  143. !DiceView methodsFor: 'displaying'!
  144.  
  145. displayView
  146.     "Display the model.  Re-create the cached forms if the display
  147.      box has changed."
  148.  
  149.     (cacheBox isNil or: [cacheBox ~= self insetDisplayBox]) ifTrue: [
  150.         self makeForms.
  151.         cacheBox _ self insetDisplayBox copy].
  152.     (cachedForms at: self model value)
  153.         displayOn: Display
  154.         at: self insetDisplayBox topLeft!
  155.  
  156. update: aParameter
  157.     "Ignore aParameter, and recreate the display."
  158.  
  159.     self display! !
  160.  
  161. !DiceView methodsFor: 'private'!
  162.  
  163. makeForms
  164.     "Construct the cached forms used for displaying."
  165.  
  166.     | box center tempForm |
  167.     box _ self insetDisplayBox.
  168.     tempForm _ Form dotOfSize: (box extent x min: box extent y) // 5.
  169.     1 to: 6 do: [:each |
  170.         cachedForms at: each put: (Form extent: box extent).
  171.         box _ (cachedForms at: each) boundingBox.
  172.         center _ box center.
  173.         (#(1 3 5) includes: each) ifTrue: [
  174.             tempForm
  175.                 displayOn: (cachedForms at: each)
  176.                 at: center].
  177.         (#(2 3 4 5 6) includes: each) ifTrue: [
  178.             tempForm
  179.                 displayOn: (cachedForms at: each)
  180.                 at: center - ((center - box topLeft) // 2).
  181.             tempForm
  182.                 displayOn: (cachedForms at: each)
  183.                 at: center + ((center - box topLeft) // 2)].
  184.         (#(4 5 6) includes: each) ifTrue: [
  185.             tempForm
  186.                 displayOn: (cachedForms at: each)
  187.                 at: center - ((center - box topRight) // 2).
  188.             tempForm
  189.                 displayOn: (cachedForms at: each)
  190.                 at: center + ((center - box topRight) // 2)].
  191.         (each = 6) ifTrue: [
  192.             tempForm
  193.                 displayOn: (cachedForms at: each)
  194.                 at: center - ((center - box leftCenter) // 2).
  195.             tempForm
  196.                 displayOn: (cachedForms at: each)
  197.                 at: center + ((center - box leftCenter) // 2)]]! !
  198. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  199.  
  200. DiceView class
  201.     instanceVariableNames: ''!
  202.  
  203.  
  204. !DiceView class methodsFor: 'instance creation'!
  205.  
  206. open 
  207.     "Create a new instance on the receiver with a new model"
  208.     "DiceView open."
  209.  
  210.     self openOn: Dice new!
  211.  
  212. openOn: aDice 
  213.     "Create a new instance on the receiver with the model aDice."
  214.     "DiceView openOn: Dice new."
  215.  
  216.     | topView diceView |
  217.     topView _ StandardSystemView
  218.                 model: nil
  219.                 label: nil
  220.                 minimumSize: 66 @ 66.
  221.     diceView _ self new model: aDice.
  222.     diceView insideColor: Form white.
  223.     diceView borderWidth: 1.
  224.     topView addSubView: diceView.
  225.     topView controller open! !
  226.